<template>
	<view class="goods-detail-box">
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,i) in goods_info.goods_pics" :key="i">
				<image :src="item"></image>
			</swiper-item>
		</swiper>
		<view class="goods-price">￥{{goods_info.goods_price}}</view>
		<view class="goods-name-box">
			<view class="goods-name">{{goods_info.goods_name}}</view>
			<view class="favi">
				<uni-icons type="star"></uni-icons>
				<text>收藏</text>
			</view>
		</view>
		<view class="goods-introduce">
			<image :src="goods_info.goods_introduce" mode="widthFix"></image>
		</view>
	</view>
	<view class="goods-nav">
		<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	import { mapGetters } from 'vuex'
	import { mapState } from 'vuex'
	export default {
		data() {
			return {
				goods_info:{},
				goods_id:0,
				options: [{
							icon: 'headphones',
							text: '客服'
						}, {
							icon: 'shop',
							text: '店铺',
							infoColor:"red"
						}, {
							icon: 'cart',
							text: '购物车',
							info: 0
						}],
					    buttonGroup: [{
					      text: '加入购物车',
					      backgroundColor: '#008000',
					      color: '#fff'
					    },
					    {
					      text: '立即购买',
					      backgroundColor: '#00b26a',
					      color: '#fff'
					    }
					    ]
			};
		},
		watch:{
			total:{
				handler(newVal){
					const findResult = this.options.find(x=>x.text=='购物车')
					if (findResult){
						findResult.info = newVal
					}
				},
				immediate:true
			}
		},
		onLoad(option){
			this.getGoodsInfo(option.goods_id)
			
		},
		computed:{
			...mapGetters('cart',['total']),
			...mapState('cart',['cartList'])
		},
		methods:{
			...mapMutations('cart', ['addToCart']),
			async getGoodsInfo(goods_id){
				const {data:res} = await uni.$http.get('/api/public/v1/goods/detail?id='+goods_id)
				if (res.meta.status!==200){
					uni.$showMsg()
				}else{
					/*res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(/webp/g, 'jpg')
					this.goods_info = res.message*/
					this.goods_info = res.message
				}
			},
			onClick (e) {
				if (e.content.text=='购物车'){
					uni.switchTab({
						url:'/pages/cart/cart'
					})
				}
			},
			buttonClick (e) {
				if (e.content.text=='加入购物车'){
					this.addToCart({
						goods_id:this.goods_info.id,
						goods_name:this.goods_info.goods_name,
						goods_price:this.goods_info.goods_price,
						goods_count:1,
						goods_icon:this.goods_info.goods_icon,
						goods_state:true
					})
					
					uni.$showMsg("已加入购物车")
				}
			}
		}
	}
</script>

<style lang="scss">
	
	swiper{
		height: 750rpx;
		image{
			width: 100%;
			height: 100%;
		}
	}
	.goods-detail-box{
		background-color: white;
		padding-bottom: 50px;
	}
	.goods-price{
		color: #008000;
		font-size: 28px;
		margin-top: 3px;
		margin-left: 10px;
	}
	.goods-name-box{
		display: flex;
		font-size: 12px;
		align-items: center;
		justify-content: space-between;
		margin-bottom: 100px;
		.goods-name{
			width: 650rpx;
			border-right: 1px #efefef solid;
		}
		.favi{
			width: 100rpx;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
		}
	}
	.goods-introduce{
		margin-top: 10px;
	}
	.goods-nav{
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		//position: -webkit-sticky;
		//position: sticky;
		//bottom: 0;
	}
</style>
